<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Console\Controller;

/**
 * 后台分类管理控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */

class CrmController extends ConsoleController {

	public function _initialize(){
        parent::_initialize();
		$this->assign('currentGroup',$this->getMenuCurrent(self::$nodes));
    }
    static protected $allow = array( 'tree');
    /**
     * 左侧导航节点定义
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    static protected $nodes = array(

        /* 导航栏目设置 */
        array( 'title' => '新鲜客户', 'url' => 'Crm/index', 'group' => '客户管理',
            'operator'=>array(
                array('title'=>'编辑','url'=>'Postition/edit','tip'=>'编辑和保存栏目分类'),
            	array('title'=>'新增','url'=>'Category/add','tip'=>'新增栏目分类'),
            	array('title'=>'删除','url'=>'Category/remove','tip'=>'删除栏目分类'),
            ),
        ),
		array( 'title' => '意向客户', 'url' => 'Crm/intention', 'group' => '客户管理'),
		array( 'title' => 'VIP客户', 'url' => 'Crm/vip', 'group' => '客户管理'),
    );
	
    /**
     * 分类管理列表
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    public function index(){
		/* 查询条件初始化 */
    	$map = array();
        $map  = array('customer_type'=>'fresh','is_deleted' => 0);
        if(isset($_GET['name'])){
        	$map['name']  = array('like', '%'.(string)I('name').'%');
        }
		$Customer = D('Crm');
		$list = $this->lists($Customer, $map);

		foreach($list as $k => $v){
			$list[$k]['customer_type'] = '新鲜客户';
		}
        $this->assign('list', $list);
        $this->main_title = '新鲜客户-客户管理';
		$this->meta_title = '新鲜客户';
		//获取用户IDecho 'id'.session('user_auth.uid').;
        $this->display();
    }
    /**
     * 意向客户列表
     * @author sunguide <sunguide@qq.com>
     */
    public function intention(){
        /* 查询条件初始化 */
    	$map = array();
        $map  = array('customer_type'=>'intention','is_deleted' => 0);
        if(isset($_GET['name'])){
        	$map['name']  = array('like', '%'.(string)I('name').'%');
        }
		$Customer = D('Crm');
		$list = $this->lists($Customer, $map);
		foreach($list as $k => $v){
			$list[$k]['customer_type'] = '意向客户';
		}
		//dump($tree);
        $this->assign('list', $list);
        $this->main_title = '意向客户-分类管理';
		$this->meta_title = "意向客户";
        $this->display('index');
    }
	 /**
     * VIP客户列表
     * @author sunguide <sunguide@qq.com>
     */
    public function vip(){
         /* 查询条件初始化 */
    	$map = array();
        $map  = array('customer_type'=>'vip','is_deleted' => 0);
        if(isset($_GET['name'])){
        	$map['name']  = array('like', '%'.(string)I('name').'%');
        }
		$Customer = D('Crm');
		$list = $this->lists($Customer, $map);
		foreach($list as $k => $v){
			$list[$k]['customer_type'] = 'VIP客户';
		}
		
        $this->assign('list', $list);
        $this->main_title = "VIP客户-客户管理";
		$this->meta_title = "VIP客户";
        $this->display('index');
    }
	/* 客户跟踪 */
    public function track($id = 0){
        $Crm = D('Crm');
        if(IS_POST){ //提交表单
            if(false !== $Crm->track()){
				$in = $Crm->getLastSql();
                $this->success('新增成功！', U('index'));
            } else {
                $error = $Crm->getError();
                $this->error(empty($error) ? '未知错误！' : $error);
            }
        } else {
			/* 获取客户跟踪信息 */
			$track = $id ? $Crm->trackRecords($id) : '';
            /* 获取分类信息 */
            $info = $id ? $Crm->info($id) : '';
			//dump($track);
			$this->assign('track',$track);
            $this->assign('info',       $info);
			$this->assign('tabNav', array('tab1'=>true,'tab2'=>true,'tab3'=>''));
			$this->main_title = "客户跟踪记录";
            $this->display('edit');
        }
    }


    /* 编辑分类 */
    public function edit($id = null, $pid = 0){
        $Crm = D('Crm');

        if(IS_POST){ //提交表单
            if(false !== $Crm->update()){
                $this->success('编辑成功！', U('index'));
            } else {
                $error = $Crm->getError();
                $this->error(empty($error) ? '未知错误！' : $error);
            }
        } else {
            $cate = '';
            if($pid){
                /* 获取上级分类信息 */
                $cate = $Crm->info($pid, 'id,name,title,status');
                if(!($cate && 1 == $cate['status'])){
                    $this->error('指定的上级分类不存在或被禁用！');
                }
            }

            /* 获取分类信息 */
            $info = $id ? $Crm->info($id) : '';

            $this->assign('info',       $info);
            $this->assign('category',   $cate);
			$this->main_title = "编辑客户";
            $this->meta_title = '编辑分类';
            $this->display();
        }
    }
    /* 新增分类 */
    public function add($pid = 0){
        $Crm = D('Crm');

        if(IS_POST){ //提交表单
            if(false !== $Crm->update()){
				$in = $Crm->getLastSql();
                $this->success('新增成功！', U('index'));
            } else {
                $error = $Crm->getError();
                $this->error(empty($error) ? '未知错误！' : $error);
            }
        } else {

            $cate = array();
            if($pid){
                /* 获取上级分类信息 */
                $cate = $Crm->info($pid, 'id,name,title,status');
                if(!($cate && 1 == $cate['status'])){
                    $this->error('指定的上级分类不存在或被禁用！');
                }
            }
            /* 获取分类信息 */
            $this->assign('position', $cate);
			$this->main_title = "新增客户";
            $this->meta_title = '新增分类';
			$this->assign('tabNav', array('tab1'=>'','tab2'=>true,'tab3'=>true));
            $this->display('edit');
        }
    }

	/**
     * 删除一个联系人
     * @author sunguide <sunguide@qq.com>
     */
    public function remove(){
		$Crm = D('Crm');
        $cate_id = I('id');
        if(empty($cate_id)){
            $this->error('参数错误!');
        }
		if(is_array($cate_id)){
			//删除该组联系人信息
			foreach($cate_id as $id){
				//先删除对该客户有没有跟踪记录 
				$CustomerTrack = M('CustomerTrack','crm_');
				$v = $CustomerTrack->where("customer_id = $cate_id")->find();
				if(!$v || $CustomerTrack->where("customer_id = $cate_id")->save(array('is_deleted'=>1))){
					$data = array(
						'id'	 =>$id,
						'is_deleted' => 1
					);
					$res = $Crm->save($data);
				}else{
					$this->error('请先删除客户的跟踪记录');
				}
				
			}
		}else{
			$cate_id = intval($cate_id);
			//先删除对该客户有没有跟踪记录 
			$CustomerTrack = M('CustomerTrack','crm_');
			$v = $CustomerTrack->where("customer_id = $cate_id")->find();
			if(!$v || $CustomerTrack->where("customer_id = $cate_id")->save(array('is_deleted'=>1))){
				//删除该联系人信息
				$data = array(
					'id'	 =>$cate_id,
					'is_deleted' => 1
				);
				$res = $Crm->save($data);
			}else{
				$this->error('请先删除客户的跟踪记录');
			}
		}
        
        if($res !== false){
            $this->success('删除客户成功！');
        }else{
            $this->error('删除客户失败！');
        }
    }
    
}
